package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zprmb.udzt.Back_money_new;
import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.Uplan_new;
import com.zprmb.udzt.util.Regular;

public class Regular_new_Adapter extends BaseAdapter {
	private Context context;
	private List<Uplan_new> list;
	private String title;
	DecimalFormat df = new DecimalFormat("###0.00");
	 DecimalFormat df2 = new DecimalFormat("###0");
	 DecimalFormat df3 = new DecimalFormat("###0.0");
    private long minute=0;
    private long second=0;
    Help h=null;
    long keyong=0;
    long cha=-1000;
	private boolean flag=false;
	
    
	
	


	public Regular_new_Adapter(Context context, List<Uplan_new> list, String title,
			boolean flag) {
		super();
		this.context = context;
		this.list = list;
		this.title = title;
		this.flag = flag;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView==null){
			h=new Help();			
			Log.e("title", title);
			if(title.equals("募集中")){
				convertView=LayoutInflater.from(context).inflate(R.layout.uplan_new, null);
			    
			}else if(title.equals("募集结束")){
				convertView=LayoutInflater.from(context).inflate(R.layout.uplan_new, null);
			  
			} else if(title.equals("理财中")){
				convertView=LayoutInflater.from(context).inflate(R.layout.uplan_new, null);
			}else if(title.equals("已回款")){
				convertView=LayoutInflater.from(context).inflate(R.layout.uplan_new, null);
			}
			
			h.tv_title=(TextView)convertView.findViewById(R.id.conduct_name);
			h.tv_addrate=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.tv_out_money=(TextView)convertView.findViewById(R.id.ying_outmoney);
			h.invest_info=(TextView)convertView.findViewById(R.id.invest_info);
			h.product_info=(TextView)convertView.findViewById(R.id.product_info);
			h.tv_investamount=(TextView)convertView.findViewById(R.id.amount);
			h.li_out_money=(LinearLayout)convertView.findViewById(R.id.li_out_money);
			h.tv_count_time=(TextView)convertView.findViewById(R.id.count_time);
			h.li_progress=(LinearLayout)convertView.findViewById(R.id.li_progress);
			h.tv_progress=(TextView)convertView.findViewById(R.id.tv_progress);
			h.tv_conduct_addrate_pl=(TextView)convertView.findViewById(R.id.conduct_addrate_pl);
			h.sk_progresss=(ProgressBar)convertView.findViewById(R.id.sk_progresss);
			h.tv_conduct_addrate=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.tv_status=(TextView)convertView.findViewById(R.id.out_money_status);
			h.tv_back_plan=(TextView)convertView.findViewById(R.id.out_money);
			h.rl_back_money=(RelativeLayout)convertView.findViewById(R.id.rl_back_money);
			h.li_layout=(LinearLayout)convertView.findViewById(R.id.li_layout);
			
			
			convertView.setTag(h);
		}else{
			h=(Help) convertView.getTag();
		}
		if(title.equals("募集中")){
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getAmount())));
			h.tv_out_money.setText(list.get(position).getDueRepayAmount());
			h.li_progress.setVisibility(View.VISIBLE);
			
			
			h.li_out_money.setVisibility(View.GONE);
			h.tv_back_plan.setVisibility(View.GONE);
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getToAccountTime())+"到期");
			h.product_info.setText("项目信息:"+df.format(Double.parseDouble(list.get(position).getFixedRate())-Double.parseDouble(list.get(position).getFixedRateAdd()))+"％预期年化收益率/期限"+list.get(position).getDays()+"天/"+list.get(position).getPlanRepaymentTypeName());
		   
		  
		   
		 
		}else if(title.equals("募集结束")){
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getAmount())));
			h.li_out_money.setVisibility(View.GONE);
			h.tv_back_plan.setVisibility(View.GONE);
			h.tv_count_time.setVisibility(View.VISIBLE);
			h.tv_count_time.setText(Regular.getMilliToDate4(list.get(position).getCountRateTime())+"开始计息");
			Log.e("count_time", h.tv_count_time.getText().toString()+""+(h.tv_count_time.getVisibility()==View.VISIBLE)+""+(h.rl_back_money.getVisibility()==View.VISIBLE));
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getToAccountTime())+"到期");
		
			h.product_info.setText("项目信息:"+df.format(Double.parseDouble(list.get(position).getFixedRate())-Double.parseDouble(list.get(position).getFixedRateAdd()))+"％预期年化收益率/期限"+list.get(position).getDays()+"天/"+list.get(position).getPlanRepaymentTypeName());
			h.tv_out_money.setText(list.get(position).getDueRepayAmount());
			
		} else if(title.equals("理财中")){			
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getAmount())));
			h.tv_out_money.setText(list.get(position).getDueRepayAmount());
			h.tv_count_time.setText("请去官网查看投资协议");
			h.tv_status.setText("预期回款金额(元)");
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getCountRateTime())+"计息/"+Regular.getMilliToDate3(list.get(position).getToAccountTime())+"到期");
			h.product_info.setText("项目信息:"+df.format(Double.parseDouble(list.get(position).getFixedRate())-Double.parseDouble(list.get(position).getFixedRateAdd()))+"％预期年化收益率/期限"+list.get(position).getDays()+"天/"+list.get(position).getPlanRepaymentTypeName());
			h.rl_back_money.setVisibility(View.VISIBLE);
		}else if(title.equals("已回款")){		
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getAmount())));			
			h.tv_out_money.setText(list.get(position).getRepayAmount());
			h.tv_count_time.setText("请去官网查看投资协议");
			h.tv_status.setText("已回款金额(元)");
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getCountRateTime())+"计息/"+Regular.getMilliToDate3(list.get(position).getToAccountTime())+"到期");
			h.product_info.setText("项目信息:"+df.format(Double.parseDouble(list.get(position).getFixedRate())-Double.parseDouble(list.get(position).getFixedRateAdd()))+"％预期年化收益率/期限"+list.get(position).getDays()+"天/"+list.get(position).getPlanRepaymentTypeName());
			h.rl_back_money.setVisibility(View.VISIBLE);
		}
		h.tv_title.setText(list.get(position).getProductName());
		h.sk_progresss.setProgress(Integer.parseInt(df2.format(list.get(position).getDegree()*1000)));
		h.tv_progress.setText(df3.format(list.get(position).getDegree()*100)+"%");
		if(Double.parseDouble(list.get(position).getFixedRateAdd())>0){
			h.tv_conduct_addrate_pl.setText("平台加息"+list.get(position).getFixedRateAdd()+"%");
		}else{
			h.tv_conduct_addrate_pl.setVisibility(View.GONE);
		}
		if(list.get(position).getRaisingRates()>0){
			h.tv_conduct_addrate.setVisibility(View.VISIBLE);
			h.tv_conduct_addrate.setText("券加息"+list.get(position).getRaisingRates()+"%");
		}else{
			h.tv_conduct_addrate.setVisibility(View.GONE);
		}

		h.tv_back_plan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context,Back_money_new.class);
				intent.putExtra("investId", list.get(position).getInvestId());
				intent.putExtra("type", 1);
				intent.putExtra("title", title);
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}
	public String ondata(){
		SimpleDateFormat    formatter    =   new    SimpleDateFormat    ("ddHHmmss");       
		Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间       
		String    str    =    formatter.format(curDate);
		return str;
	}
	
	 class Help{
		 private TextView tv_title,tv_addrate,tv_investamount,tv_jindu,invest_info,product_info,tv_out_money,tv_status,tv_back_plan;
		 private TextView tv_v4_productdetails_day,tv_v4_productdetails_hour,tv_v4_productdetails_minutes,tv_v4_productdetails_second;
		private ProgressBar sk_progresss;
		private TextView tv_progress;
		private TextView tv_count_time,tv_conduct_addrate,tv_conduct_addrate_pl;
		 private LinearLayout li_out_money,li_layout,li_progress;
		 private RelativeLayout rl_back_money;
	 }
	 

}
